package com.csdj.rest.controller;

import com.csdj.beans.entity.Order;
import com.csdj.beans.interfaces.order.IOrderService;
import com.csdj.beans.interfaces.pay.IPayService;
import com.csdj.common.WebResult;
import com.csdj.common.constant.CommonConstant;
import com.csdj.common.constant.WebResultConstant;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pay")
public class PayController {

//    @DubboReference
    private IPayService payService;
//    @DubboReference
    private IOrderService orderService;

    @PostMapping("/{orderId}")
    public WebResult payOrder(@PathVariable("orderId")int orderId, HttpServletRequest request){
        String uidStr = request.getHeader(CommonConstant.HEADER_USER_ID);
        int uid = Integer.parseInt(uidStr);
        final Order order = orderService.getById(orderId);
        if(order != null){
            //判断订单的用户是否一致
            if(order.getUserId() != uid){
                return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS);
            }
            if(payService.startPayment(order,(byte)2)){
                return WebResult.success(null);
            }
            return WebResult.fail(WebResultConstant.PAY_FAIL);
        }else{
            return WebResult.fail(WebResultConstant.ORDER_NOT_EXISTS);
        }
    }
}
